1 module about_classes; 2 3 import dunit; 4 import helpers; 5 6 class A { 7 private int myValue; // not accessible from outside 8 9 this (int startValue) { // constructor 10 myValue = startValue; 11 } 12 13 } 14 15 class B: A { 16 this() { 17 super(3); // what happens here ? 18 } 19 20 auto getDoubleValue() { 21 return myValue * 2; // B doesn't have this field but.. 22 } 23 } 24 25 class AboutClasses { 26 mixin UnitTest; 27 28 @Test 29 public void inheritance() { 30 auto instance = new B; 31 assertEquals(instance.getDoubleValue(), FILL_IN_THIS_NUMBER); 32 } 33 34 // code preparation for next test 35 struct TimeOfDay_s { 36 int hour; 37 int minute; 38 } 39 40 class TimeOfDay_c { 41 int hour; 42 int minute; 43 } 44 45 void set_time(TimeOfDay_c tc,int hour, int min) { 46 tc.hour=hour; 47 tc.minute=min; 48 } 49 50 void set_time(TimeOfDay_s ts,int hour, int min) { 51 ts.hour=hour; 52 ts.minute=min; 53 } 54 55 56 @Test 57 public void class_vs_struct() { 58 TimeOfDay_s timestruct; // memory allocated on the stack 59 TimeOfDay_c timeclass; 60 // here timeclass it's a null pointer! 61 // must allocate space and initialize fields... 62 timeclass=new TimeOfDay_c; // object is created on the heap 63 set_time(timestruct,15,10); 64 set_time(timeclass,15,10); 65 66 assertEquals(timestruct.hour,FILL_IN_THIS_NUMBER); //surprised ? 67 assertEquals(timeclass.hour,FILL_IN_THIS_NUMBER); //classes are reference type 68 } 69 }